package com.steel.yfmall.cloud.assistant.user.impl;

import com.steel.yfmall.cloud.assistant.dto.para.user.ParaSteelUserCompanyRegionRel;
import com.steel.yfmall.cloud.assistant.po.user.SteelUserCompanyRegionRel;
import com.steel.yfmall.cloud.assistant.po.user.SteelUserCompanyRegionRelExample;
import com.steel.yfmall.cloud.assistant.user.UserCompanyRegionRelService;
import com.steel.yfmall.cloud.assistant.user.mapper.SteelUserCompanyRegionRelMapper;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: UserCompanyRegionRelServiceImpl
 * @author: liuzhijun
 * @date: 2021-05-10 17:19
 **/
@Service
@Slf4j
public class UserCompanyRegionRelServiceImpl implements UserCompanyRegionRelService {

    @Resource
    private SteelUserCompanyRegionRelMapper userCompanyRegionRelMapper;

    /**
     * 苍穹同步公司终到地点终到地区信息 (全量推送,未推送数据需要删除)
     *
     * @param paraList
     * @return com.steel.yfmall.cloud.components.utils.base.APIMsgBean<java.lang.Boolean>
     * @author liuzhijun
     * @date 2021/5/10 19:14
     */
    @Override
    public APIMsgBean<Boolean> companyRegionSyncStatus(ParaSteelUserCompanyRegionRel paraList) {
        APIMsgBean<Boolean> booleanAPIMsgBean = new APIMsgBean<>();
        booleanAPIMsgBean.setData(false);
        booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean());
        if (paraList == null || paraList.getCompanyRegionRelList() == null || CollectionUtils.isEmpty(paraList.getCompanyRegionRelList())) {
            booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("必传参数为空"));
            return booleanAPIMsgBean;
        }
        List<SteelUserCompanyRegionRel> newList = new ArrayList<>();
        for (ParaSteelUserCompanyRegionRel item : paraList.getCompanyRegionRelList()) {
            SteelUserCompanyRegionRel steelUserCompanyRegionRel = new SteelUserCompanyRegionRel();
            MobileBaseBeanUtils.copyAllFields(item, steelUserCompanyRegionRel);
            steelUserCompanyRegionRel.setCreateTime(new Date());
            newList.add(steelUserCompanyRegionRel);
        }
        ParaSteelUserCompanyRegionRel para = new ParaSteelUserCompanyRegionRel();
        para.setCompanyCode(paraList.getCompanyRegionRelList().get(0).getCompanyCode());
        List<SteelUserCompanyRegionRel> oldList = getSteelUserCompanyRegionRelByList(para);
        if (oldList != null && !CollectionUtils.isEmpty(oldList)) {
            //差集新增
            List<SteelUserCompanyRegionRel> addList = newList.stream()
                    .filter(item -> !oldList.stream().map(e -> e.getCompanyCode() + e.getStorageCode() + e.getStorageType() + e.getStatus()).collect(Collectors.toList()).contains(item.getCompanyCode() + item.getStorageCode() + item.getStorageType() + item.getStatus()))
                    .collect(Collectors.toList());
            //差集删除
            List<SteelUserCompanyRegionRel> deleteList = oldList.stream()
                    .filter(item -> !newList.stream().map(e -> e.getCompanyCode() + e.getStorageCode() + e.getStorageType() + e.getStatus()).collect(Collectors.toList()).contains(item.getCompanyCode() + item.getStorageCode() + item.getStorageType() + item.getStatus()))
                    .collect(Collectors.toList());
            if (addList != null && !CollectionUtils.isEmpty(addList)) {
                batchInsertByList(addList);
            }
            if (deleteList != null && !CollectionUtils.isEmpty(deleteList)) {
                batchDeleteByList(deleteList);
            }
            booleanAPIMsgBean.setData(true);
            booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
        } else {
            if (batchInsertByList(newList)) {
                booleanAPIMsgBean.setData(true);
                booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
            }
        }
        return booleanAPIMsgBean;
    }


    @Override
    public boolean batchInsertByList(List<SteelUserCompanyRegionRel> recordList) {
        if (recordList == null || CollectionUtils.isEmpty(recordList)) {
            return false;
        }
        return userCompanyRegionRelMapper.batchInsertByList(recordList) > 0;
    }


    @Override
    public boolean batchDeleteByList(List<SteelUserCompanyRegionRel> recordList) {
        if (recordList == null || CollectionUtils.isEmpty(recordList)) {
            return false;
        }
        return userCompanyRegionRelMapper.batchDeleteByList(recordList) > 0;
    }

    @Override
    public List<SteelUserCompanyRegionRel> getSteelUserCompanyRegionRelByList(ParaSteelUserCompanyRegionRel paraBean) {
        SteelUserCompanyRegionRelExample example = assemblyParameters(paraBean);
        return userCompanyRegionRelMapper.selectByExample(example);
    }

    private SteelUserCompanyRegionRelExample assemblyParameters(ParaSteelUserCompanyRegionRel paraBean) {
        SteelUserCompanyRegionRelExample example = new SteelUserCompanyRegionRelExample();
        SteelUserCompanyRegionRelExample.Criteria criteria = example.createCriteria();
        if (paraBean == null) {
            return example;
        }
        if (StringUtils.isNotBlank(paraBean.getCompanyCode())) {
            criteria.andCompanyCodeEqualTo(paraBean.getCompanyCode());
        }
        return example;
    }


}
